// pages/my/my.js
var Bmob = require('../../dist/Bmob-1.6.0.min.js');
const {
  $Message
} = require('../../dist/base/index');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    avatarUrl:null,
    name:null,
    isOk:false
  },
  showIcard:function(){
    wx.navigateTo({
      url: '../../pages/idcard/idcard?objectId=' + app.globalData.authData.objectId
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    that.setData({
      userInfo: app.globalData.userInfo,
      userId: app.globalData.authData.objectId
    });
    console.log('访问 user', that.data.userId, '的个人信息')
    const pointer = Bmob.Pointer('_User')
    const poiID = pointer.set(that.data.userId)

    const query = Bmob.Query('UserData')
    query.include('major', 'post')
    //userId 字段名称关联用户表 ，类型Pointer
    query.equalTo("user", "==", poiID);
    query.find().then(res => {
      //获取到用户add信息之后先获取用户的微信昵称和头像做默认值，如果add信息里面有相同内容覆盖
      const query1 = Bmob.Query('_User')
      query1.get(that.data.userId).then(res1 => {
        console.log("res1", res1)
        console.log("res", res)
        //设置默认值
        that.setData({
          name: res1.nickName,
          avatarUrl: res1.userPic,
        })
        if (res.length) {
          console.log("查询到用户的UserData 显示服务器的信息")
          that.setData({
            name: res[0].nickName,
            avatarUrl: res[0].userPic.url,
          })
        }
        else {
          console.log("未查询到用户的UserData 显示用户的默认信息")
        }
        that.setData({
          isOk:true
        })

      }).catch(err => {
        console.log(err)
      })
    })


  },
  myPost:function(){
    wx.navigateTo({
      url: '../../pages/myPost/myPost'
    })
  },
  message: function () {
    wx.navigateTo({
      url: '../../pages/message/message'
    })
  },
  myGroup: function () {
    wx.navigateTo({
      url: '../../pages/myGroups/myGroups'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})